<template>
  <el-dialog
    :title="'字典项设置--'+typeName"
    :visible="visible"
    width="850px"
    class="dialog—with-scroll"
    @close="close"
  >
    <el-container class="tab-container">
      <el-main class="main">
        <collapse-tab class="main-search">
          <el-form
            :inline="true"
            :model="queryCondition"
            label-width="80px"
            @keyup.enter.native="query"
          >
            <!--查询条件区 -->
            <el-form-item label="名称">
              <p-input v-model="queryCondition.name" class="form-item" />
            </el-form-item>
            <el-form-item label="编码">
              <p-input v-model="queryCondition.code" class="form-item" />
            </el-form-item>
            <el-form-item label="状态">
              <dictionary-select
                v-model="queryCondition.status"
                multiple
                :code="constant.DICTIONARY_TYPE_CODES.STATUS"
              />
            </el-form-item>
            <el-form-item style="float:right;">
              <list-button-query :page-code="pageCode" :click-event="loadCustumData" />
            </el-form-item>
            <div class="clearfix" />
          </el-form>
        </collapse-tab>
        <el-card class="main-report">
          <div>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-plus"
              style="float:left;"
              @click="customAdd"
            >新增</el-button>
            <el-button
              v-if="false"
              size="small"
              type="primary"
              icon="el-icon-delete"
              style="float:left;"
              @click="customBatchRemove"
            >删除</el-button>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-check"
              style="float:left;"
              @click="customSave"
            >保存</el-button>
            <div style="clear:both;" />
          </div>
          <div style="margin-top:5px;">
            <el-table
              v-loading="loading"
              :data="tableData"
              style="width: 100%"
              highlight-current-row
              border
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="55" />

              <el-table-column>
                <template slot="header">
                  <span style="color:red;">*</span>
                  名称
                </template>
                <template slot-scope="scope">
                  <el-input v-model="scope.row.name" />
                </template>
              </el-table-column>
              <el-table-column>
                <template slot="header">
                  <span style="color:red;">*</span>
                  编码
                </template>
                <template slot-scope="scope">
                  <el-input v-model="scope.row.code" />
                </template>
              </el-table-column>
              <el-table-column width="120px">
                <template slot="header">状态</template>
                <template slot-scope="scope">{{ scope.row.statusName }}</template>
              </el-table-column>
              <el-table-column label="排序" width="80">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.orderNo" />
                </template>
              </el-table-column>
              <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                  <el-button type="text" @click="customRemove(scope.$index)">删除</el-button>
                  <table-button-enable :id="scope.row.id" :page-code="pageCode" />
                  <table-button-disable :id="scope.row.id" :page-code="pageCode" />
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-main>
    </el-container>
  </el-dialog>
</template>

<script>
import { listMixin } from '@/mixin/listMixin'
const MODULE_CODE = 'system'
const ENTITY_TYPE = 'dictionaryItem'
export default {
  name: ENTITY_TYPE,

  mixins: [listMixin],
  data() {
    return {
      entityType: ENTITY_TYPE,
      moduleCode: MODULE_CODE,
      // eslint-disable-next-line no-eval
      api: eval('this.$api.' + MODULE_CODE + '.' + ENTITY_TYPE),
      pageCode: MODULE_CODE + ':' + ENTITY_TYPE + ':',
      queryCondition: {
        status: [this.constant.STATUS.NORMAL]
      },
      visible: false,
      typeId: '',
      typeName: ''
    }
  },
  methods: {
    show(id, name) {
      this.typeId = id
      this.typeName = name
      this.loadCustumData()
      this.visible = true
    },
    loadCustumData() {
      this.loading = true
      const params = Object.assign(this.queryCondition, this.sortInfo, { typeId: this.typeId })

      this.api.list(params)
        .then(res => {
          this.tableData = res.data
        }).finally(() => {
          this.loading = false
        })
    },
    customAdd() {
      this.tableData.push({ code: '', name: '', typeId: this.typeID, status: this.constant.STATUS.NORMAL, statusName: '正常', orderNo: '' })
    },
    customRemove(index) {
      this.tableData.splice(index, 1)
    },
    customBatchRemove() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'info',
          message: '请至少选中一行'
        })
        return
      }
      this.$confirm('此操作将批量删除数据, 是否继续?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.multipleSelection.forEach(function (item) {
          this.tableData.splice(this.tableData.findIndex(x => x.id === item), 1)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    customSave() {
      this.loading = true
      this.$api.system.dictionaryType.saveItem(this.typeId, this.tableData)
        .then(() => {
          this.loadCustumData()
        }).finally(() => {
          this.loading = false
        })
    },
    close() {
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.tab-container {
  $item-width: 200px;
  padding: 15px;
  .aside {
    min-width: 180px;
    background-color: white;
  }
  .main {
    .main-search {
      .form-item {
        width: $item-width;
      }
    }
    .main-report {
      margin-top: 10px;
    }
  }
}
</style>
